<template>
  <div class="complaint-submit">
    <div class="sub-page-header">
      <el-tooltip content="点击返回帮扶政策优化方案" placement="left">
        <div class="title" @click="goBack">
          <el-button icon="el-icon-arrow-left" type="text"></el-button>
          {{ formDisabled ? '优化方案详情' : id ? '修改优化方案' : '新增优化方案' }}
        </div>
      </el-tooltip>
    </div>
    <el-scrollbar class="apply-box submit-fixed">
      <div class="content">
        <div class="form">
          <el-form
            label-suffix="："
            :model="submitForm"
            :rules="rules"
            label-width="130px"
            ref="dataForm"
            :disabled="formDisabled">
            <div class="section-title">基本信息</div>
            <el-form-item prop="planName" label="方案名称">
              <el-input
                placeholder="请输入"
                v-model="submitForm.planName"
                maxlength="120"
                show-word-limit
                clearable></el-input>
            </el-form-item>
            <el-form-item prop="planContent" label="方案详情">
              <TinymceEditor v-model="submitForm.planContent" height="400"></TinymceEditor>
            </el-form-item>
            <el-form-item label="关联政策" class="mb0">
              <!-- :prop="`relPlanList.${index}.policyId`"
                :rules="rules.policyId" -->
              <el-form-item v-for="(item, index) in submitForm.relPlanList" :key="index">
                <el-row>
                  <el-col :span="21" class="rules-select">
                    <el-select
                      v-model="item.policyId"
                      placeholder="请选择"
                      filterable
                      clearable
                      popper-class="el-select-wrap"
                      class="full_width"
                      @change="onRuleChange">
                      <el-option
                        v-for="item in relPlanDataList"
                        :key="item.id"
                        :label="item.policyName"
                        :value="item.id"
                        :disabled="item.disabled"></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="3">
                    <i class="plus el-icon-circle-plus" @click="onAddRemove('add', index)"></i>
                    <i
                      class="plus el-icon-remove"
                      v-show="submitForm.relPlanList.length > 1"
                      @click="onAddRemove('remove', index)"></i>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-form-item>
            <el-form-item prop="messageContent" label="匹配算法">
              <el-select
                clearable
                v-model="submitForm.algorithmName"
                placeholder="请选择"
                filterable
                popper-class="el-select-wrap"
                style="width: 100%">
                <el-option v-for="item in suanfaList" :key="item.id" :label="item.name" :value="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="附件信息" v-if="false">
              <UploadFile :file-list="albumList" />
            </el-form-item>
          </el-form>
        </div>
        <div v-if="!formDisabled" class="btns-view">
          <div class="btn-box">
            <el-button class="btn-item" type="primary" @click="submitData">提交</el-button>
          </div>
        </div>
      </div>
    </el-scrollbar>
  </div>
</template>

<script>
import {
  getPolicyPlanDetailApi,
  editPolicyPlanApi,
  savePolicyPlanApi,
  listAllRulesApi,
} from '@api/modules/helpPolicy.js'
import UploadFile from '@com/UploadFile.vue'
import TinymceEditor from '@com/TinymceEditor.vue'

export default {
  name: 'enterpriseCreate',
  components: { UploadFile, TinymceEditor },
  props: {},
  data() {
    return {
      id: '',
      submitForm: {
        planName: '',
        algorithmName: '',
        planContent: '',
        relPlanList: [
          {
            policyId: '',
            orderBy: 0,
          },
        ],
      },
      relPlanDataList: [],
      relPlanDataListAll: [],
      rules: {
        planName: [{ required: true, message: '请输入方案名称', trigger: 'blur' }],
        planContent: [{ required: true, message: '请输入方案详情', trigger: 'blur' }],
        policyId: [{ required: true, message: '请选择关联政策', trigger: 'change' }],
      },
      suanfaList: [
        { id: '1', name: '农林牧渔业分析算法' },
        { id: '2', name: '采矿业分析算法' },
        { id: '3', name: '制造业分析算法' },
        { id: '4', name: '电力、热力、燃气及水生产和供应业分析算法' },
        { id: '5', name: '建筑业分析算法' },
        { id: '6', name: '批发业分析算法' },
        { id: '7', name: '零售业分析算法' },
        { id: '8', name: '道路运输业分析算法' },
      ],
    }
  },
  computed: {
    formDisabled() {
      return this.$route.params.view
    },
  },
  created() {
    this.getListAllRulesData()
    const { id } = this.$route.params
    this.id = id
    if (this.id) {
      this.getDetailData()
    }
  },
  mounted() {},
  methods: {
    getDetailData() {
      getPolicyPlanDetailApi({ id: this.id }).then(res => {
        if (res.code === 0) {
          this.onRuleChange()
          this.submitForm = { ...this.submitForm, ...res.data }
        }
      })
    },
    // 获取政策列表
    getListAllRulesData() {
      listAllRulesApi().then(res => {
        if (res.code === 0) {
          this.relPlanDataListAll = res.data
          if (!this.id) {
            this.relPlanDataList = res.data
          }
        }
      })
    },
    // 监听规则切换
    onRuleChange() {
      const ruleIds = this.submitForm.relPlanList.map(item => item.policyId)
      if (this.relPlanDataListAll.length) {
        this.relPlanDataListAll.forEach(item => {
          if (ruleIds.includes(item.id)) {
            item.disabled = true
          } else {
            item.disabled = false
          }
        })
      }
      const listJson = JSON.stringify(this.relPlanDataListAll)
      this.relPlanDataList = JSON.parse(listJson)
    },
    submitData() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          const params = { ...this.submitForm }
          // 处理政策排序
          params.relPlanList = params.relPlanList.map((item, index) => {
            item.orderBy = index + 1
            return item
          })

          let API = null
          if (params.id) {
            API = editPolicyPlanApi
          } else {
            API = savePolicyPlanApi
          }
          API(params).then(res => {
            if (res.code === 0) {
              this.$message.success(res.data)
              this.goBack()
            }
          })
        }
      })
    },
    // 添加移除规则
    onAddRemove(type, index) {
      if (type === 'add') {
        this.submitForm.relPlanList.splice(index + 1, 0, {
          policyId: '',
          orderBy: 0,
        })
      } else {
        this.submitForm.relPlanList.splice(index, 1)
      }
      this.onRuleChange()
    },
    goBack() {
      this.$router.back(-1)
    },
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.onWindowResize)
  },
}
</script>

<style lang="scss" scoped>
.complaint-submit {
  height: 100%;
  @include flex-y;
  position: relative;
  background: #f5f6fa;
  padding: 20px;
  .el-scrollbar {
    @include flex-1;
  }
  ::v-deep .el-select-group {
    display: flex;
    flex-wrap: wrap;
  }

  .apply-box {
    background-color: #fff;
    .content {
      padding: 20px 30px;
    }
    .plus {
      cursor: pointer;
      font-size: 22px;
      margin-left: 10px;
    }
    .rules-select {
      display: flex;
    }
    .rule-oper {
      margin-right: 10px;
    }
    .section-title {
      font-size: 16px;
      color: #333;
      font-weight: bold;
      padding: 10px;
      background: #f0f4f9;
      margin-bottom: 15px;
    }
    ::v-deep .form {
      .el-form-item {
        margin-bottom: 24px;
      }
    }
  }
  .btns-view {
    // @include flex-x(center, center);
    text-align: center;
    border-top: 1px solid $-border-color-base;
    margin-top: 10px;
    padding: 20px;
    background-color: #fff;
    .other-btn-box {
      margin-top: 10px;
      text-align: center;
    }
    .btn-item {
      width: 120px;
      height: 40px;
      font-size: 16px;
    }
    .empty {
      background-color: transparent;
      color: #4a79ba;
    }
  }
  .submit-fixed {
    padding-bottom: 100px;
    .btns-view {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
    }
  }

  @media screen and(max-width:800px) {
    .apply-box {
      .content {
        padding: 20px;
      }
    }
    .msg-container {
      .section-title {
        padding: 20px 50px;
        font-size: 36px;
        .icon-img-ts,
        .icon-img-person {
          display: inline-block;
          height: 36px;
          vertical-align: middle;
          margin-right: 10px;
          margin-top: -5px;
        }
      }
    }
    .el-input__count {
      font-size: 24px;
    }
  }
}
</style>
